﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.DomainService;
using Super.Entities;
using Super.Infrastructure;
using Super.Mvc;
using Super.Common;

namespace Super.Web.Areas.Admin.Controllers
{
    public class PayController : BaseController
    {
        //
        // GET: /Admin/Pay/

        public ActionResult Index()
        {
            var adp = new DbContextAdapter();
            var payService = new PayService(adp);
            var page = Query.Params("pageNum");
            var orderField = Query.Post("orderField");
            var orderDirection = Query.Post("orderDirection");


            if (string.IsNullOrEmpty(page))
            {
                page = "1";
            }
            var keyword = Query.Post("keyword");
            var moneyChangeType = Query.Post("ChangeType").ToInt(0);
            var startTime = Query.Post("startTime").ToDateTime();

            var where = PredicateBuilder.True<PayRecord>();
            where = where.And(a => a.IsDel == false);

            if (!string.IsNullOrEmpty(keyword))
            {
                where = where.And(a => a.Subject.Contains(keyword.Trim()) ||
                    a.TradeNo.Contains(keyword.Trim()) ||
                    a.OrderNo.Contains(keyword.Trim())
                    );

                ViewBag.keyword = keyword;
            }

            if (moneyChangeType > 0)
            {
                where = where.And(a => a.MoneyChangeType_ID == moneyChangeType);
            }

            if (startTime != null)
            {
                where = where.And(a => a.CreateTime > startTime);
                ViewBag.startTime = startTime;
            }

            var selector = new Func<PayRecord, dynamic>(a => a.ID);
            var produtsEnu = payService.Repository.Find(where).OrderByDescending(selector);
            var payrecords = produtsEnu.Page(page.ToInt(1), PageSize);
            BindPage(payrecords.ItemCount, page.ToInt(), PageSize);
            ViewBag.Records = payrecords.ItemCount;
            GetMoneyChangeType(moneyChangeType);

            decimal outMoney = 0;
            decimal inMoney = 0;
            foreach (PayRecord payRecord in produtsEnu)
            {
                if(payRecord.MoneyChangeType.InOut==1)
                {
                    inMoney += payRecord.Money;
                }
                if(payRecord.MoneyChangeType.InOut==2)
                {
                    outMoney += payRecord.Money;
                }

            }
            ViewBag.outMoney = outMoney;
            ViewBag.inMoney = inMoney;
            return View(payrecords.ItemCollection.ToList());
        }

        public ActionResult Edit()
        {
            GetMoneyChangeType(null);
            var id = Query.Get("id").ToInt(0);
            if (id > 0)
            {
                var adp = new DbContextAdapter();
                var payService = new PayService(adp);
                var payModel = payService.Repository.Find(a => a.ID == id).FirstOrDefault();
                return View(payModel);

            }
            return View();
        }

        [HttpPost]
        public JsonResult Edit(PayRecord model)
        {

            if (model.ID <= 0)
            {
                model.CreateTime = DateTime.Now;
                model.TradeNo = Number.GetSerialNumber(4).ToString();
            }
            var adp = new DbContextAdapter();
            var payService = new PayService(adp);
            payService.Repository.InsertOrUpdate(model);
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "payIndex");

        }

        public JsonResult Delete()
        {
            var id = Query.Get("id").ToInt(0);
            var adp = new DbContextAdapter();
            var payService = new PayService(adp);
            var payModel = payService.Repository.Find(a => a.ID == id).FirstOrDefault();
            if (payModel != null)
            {
                payService.Repository.Delete(payModel);
            }
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "payIndex");
        }

        //获取产品分类
        public void GetMoneyChangeType(int? selectValue)
        {

            var adp = new DbContextAdapter();
            var moneychangeTypeService = new MoneyChangeTypeService(adp);
            var categoryList = moneychangeTypeService.Repository.Find(a => a.IsDel == false);
            ViewBag.ChangeType = new SelectList(categoryList, "ID", "Name", selectValue);
        }

    }
}
